package com.chen.excel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@RestController
@RequestMapping("/api/waybill")
public class WaybillImageController {

    @Autowired
    private WaybillImageService waybillImageService;

    @PostMapping("/upload-excel")
    public ResponseEntity<WaybillImageService.ProcessResult> uploadExcel(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return ResponseEntity.badRequest().build();
            }

            String originalFilename = file.getOriginalFilename();
            if (originalFilename == null || (!originalFilename.endsWith(".xlsx") && !originalFilename.endsWith(".xls"))) {
                return ResponseEntity.badRequest().build();
            }

            Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
            Path tempFile = Files.createTempFile(tempDir, "waybill_", "_" + originalFilename);
            
            Files.write(tempFile, file.getBytes());

            WaybillImageService.ProcessResult result = waybillImageService.processExcelFileWithSummary(tempFile.toString());

            Files.deleteIfExists(tempFile);

            return ResponseEntity.ok(result);

        } catch (IOException e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    @PostMapping("/process-file")
    public ResponseEntity<WaybillImageService.ProcessResult> processExcelFile(@RequestBody FilePathRequest request) {
        try {
            if (request.getFilePath() == null || request.getFilePath().trim().isEmpty()) {
                return ResponseEntity.badRequest().build();
            }

            File file = new File(request.getFilePath());
            if (!file.exists() || !file.isFile()) {
                return ResponseEntity.badRequest().build();
            }

            WaybillImageService.ProcessResult result = waybillImageService.processExcelFileWithSummary(request.getFilePath());
            return ResponseEntity.ok(result);

        } catch (IOException e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    @PostMapping("/process-waybill-data")
    public ResponseEntity<List<ImageQueryService.WaybillImageResult>> processWaybillData(@RequestBody List<WaybillData> waybillDataList) {
        try {
            List<ImageQueryService.WaybillImageResult> results = waybillImageService.processWaybillDataList(waybillDataList);
            return ResponseEntity.ok(results);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    public static class FilePathRequest {
        private String filePath;

        public String getFilePath() {
            return filePath;
        }

        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }
    }
}